/*
 * 
 *  This file is part of Sourceprojects.org Thracia.
 *  Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.thracia.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author noctarius
 * 
 */
public class SHA1 {
	public static final String getSha1(byte[] data) {
		MessageDigest digest = null;
		byte[] hash = null;
		final StringBuffer result = new StringBuffer();

		try {
			digest = MessageDigest.getInstance("SHA1");
		} catch (final NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}

		digest.reset();
		digest.update(data);

		hash = digest.digest();

		for (int i = 0; i < hash.length; i++) {
			result.append(Integer.toHexString(0xFF & hash[i]));
		}

		return result.toString();
	}

	public static final String getSha1(String var1) {
		return getSha1(var1.getBytes());
	}
}
